﻿using System;
using System.Windows.Forms;
using UniMana.Utils.Helper;
using UniMana.Utils.Configs;

namespace UniMana.Win.Systems
{
    public partial class FrmLogin_SubServerConfig : DevExpress.XtraEditors.XtraForm
    {
        #region [Khai báo biến toàn cục]

        ClsRegistryOperator m_Reg = new ClsRegistryOperator();

        #endregion
        
        public FrmLogin_SubServerConfig()
        {
            InitializeComponent();
            ReadRegistry();
        }

        private void ReadRegistry()
        {

            if (m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServerConfig) == "2")
            {
                ReadReg();
            }
            else
            {
                if (m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServerConfig) == "1")
                {
                    rdgConfig.SelectedIndex = int.Parse(m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServerConfig));
                    txtEnableFalse();
                }
                else
                {
                    rdgConfig.SelectedIndex = 0;
                    DelReg();
                    txtEnableFalse();
                }
            }
            
        }

        private void ReadReg()
        {
            rdgConfig.SelectedIndex = int.Parse(m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServerConfig));
            txtServerName.Text = m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServername);
            txtDatabase.Text = m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameDataBase);
            txtLogin.Text = m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameLogin);
            txtPassSQL.Text = m_Reg.ReadRegistry(UniMana.Utils.Configs.ServerConfig.s_strNamePassSQL);
            txtEnableFalse(true);
        }

        private void DelReg()
        {
            m_Reg.DelRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServerConfig);
            m_Reg.DelRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServername);
            m_Reg.DelRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameDataBase);
            m_Reg.DelRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameLogin);
            m_Reg.DelRegistry(UniMana.Utils.Configs.ServerConfig.s_strNamePassSQL);
        }

        private void FrmLogin_SubServerConfig_Load(object sender, EventArgs e)
        {
 
            //ClsRegistryOperator clsRegOperator = new ClsRegistryOperator();

            //if (clsRegOperator.ReadRegistry("RemConnection".Encrypt()) != "")
            //{
            //    ckeSaveInfo.Checked = bool.Parse(clsRegOperator.ReadRegistry("RemConnection".Encrypt()));
            //}
            //if (ckeSaveInfo.Checked == true)
            //{
            //    txtServerName.Text = clsRegOperator.ReadRegistry("DataSource".Encrypt());
            //    txtDatabase.Text = clsRegOperator.ReadRegistry("DataBase".Encrypt());
            //    //cmbAuthentication.SelectedIndex = 1;
            //    txtLogin.Text = clsRegOperator.ReadRegistry("User ID".Encrypt());
            //    txtPassSQL.Text = clsRegOperator.ReadRegistry("Password".Encrypt());
            //}
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.AppStarting;
            SaveReg();
            CustomConnectionString();
            Close();
            Cursor = Cursors.Default;

        }

        private void SaveReg()
        {
            ClsRegistryOperator Reg = new ClsRegistryOperator();
            Reg.SaveInfoRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameServername, txtServerName.Text);
            Reg.SaveInfoRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameDataBase, txtDatabase.Text);

            if (txtLogin.Text != "" && txtPassSQL.Text != "")
            {
                Reg.SaveInfoRegistry(UniMana.Utils.Configs.ServerConfig.s_strNamePassSQL, txtPassSQL.Text);
                Reg.SaveInfoRegistry(UniMana.Utils.Configs.ServerConfig.s_strNameLogin, txtLogin.Text);
            }
            else
            {
                MessageBox.Show("Tài khoản và mật khẩu không được rỗng");
                //return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.AppStarting;
            Close();
            Cursor = Cursors.Default;
        }

        private void CustomConnectionString()
        {
            ServerConfig.s_strServername = txtServerName.Text;
            ServerConfig.s_strDataBase = txtDatabase.Text;
            ServerConfig.s_strLogin = txtLogin.Text;
            ServerConfig.s_strPassSQL = txtPassSQL.Text;
        }

        private void rdgConfig_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdgConfig.SelectedIndex == 0)
            {
                
                txtEnableFalse();
            }
            if (rdgConfig.SelectedIndex == 1)
            {
                txtEnableFalse(true);
            }
        }

        private void txtEnableFalse(bool n_False = false)
        {
            if (n_False == false)
                txtServerName.Text = txtDatabase.Text = txtLogin.Text = txtPassSQL.Text = "";
            txtServerName.Enabled= txtDatabase.Enabled = txtLogin.Enabled = txtPassSQL.Enabled = n_False;
        }

    }
}